package com.itany.service.impl;

import com.itany.exception.ServiceExecpion;
import com.itany.jdbc.factory.ObjectFactory;
import com.itany.pojo.User;
import com.itany.service.UserService;
import dao.UserDao;

import java.util.List;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月23日 15:53
 * @Description TODO
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = ObjectFactory.getObj("UserDao");

    @Override
    public User register(User u) {
        User tempUser = userDao.selectUserByUsername(u.getUsername());
        if(null!=tempUser){
            throw new ServiceExecpion("username exists");
        }
        int id = userDao.insertUserReturnPK(u);
        u.setPassword("**********");
        u.setId(id);
        return u;
    }

    @Override
    public User login(String username, String password) {
        User user = userDao.selectUserByUsernameAndPassword(username, password);
        if(user==null){
            throw new ServiceExecpion("incorrectUserNameOrPassword");
        }
        user.setPassword("*********");
        return user;
    }

    @Override
    public List<User> findUserList() {
        List<User> users = userDao.selectUserList();
        return users;
    }
}
